/*
 * Feed.java
 *
 * Created on January 5, 2008, 2:45 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package wisp.dataObjects;
import java.sql.*;
import java.io.*;
import java.util.*;
import java.lang.Exception.*;
/**
 *
 * @author Nezie Santos
 */
public class Feed {
    private dbConnection dbcon = new dbConnection();
    private String characterName, description, dateUpdated, charInv;
    private int id, battleId;
    
    /** Creates a new instance of Feed */
    public Feed() {
        battleId = 0;
    } 
    
    public Feed(String cname, String fDescription, String dUpdated, int fid, String charI) {
        setCharacterName(cname);
        setDescription(fDescription);
        setDateUpdated(dUpdated);
        setId(fid);
        charInv = charI;
        battleId = 0;
    }
    public int generateId ()
    {
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        int count = 1;
                
        getDbcon().connect();
        query = "SELECT * FROM Feed";
        
        try
        {
             	
            stmt = getDbcon().con.prepareStatement(query);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
              count++;
             }
            rs.close(); 					
            stmt.close();					
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
        return count;
    }
    
    public void addFeed (Feed f){
      						
        String query; 					
        ResultSet rs;
        PreparedStatement stmt;

        f.setId(generateId());
        getDbcon().connect();
        query = "INSERT INTO Feed (id, characterName, dateUpdated, description, charInv, battleId) VALUES (?,?,?,?,?,?)";
        try 
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setInt(1, f.getId());
            stmt.setString(2, f.getCharacterName());
            stmt.setString(3, f.getDateUpdated());
            stmt.setString(4, f.getDescription());
            stmt.setString(5, f.getCharInv());
            stmt.setInt(6, f.getBattleId());
            stmt.executeUpdate();	
            stmt.close();
        } catch (Exception ex) 
        {
            System.out.println(ex.getMessage());
        }
        getDbcon().disconnect();
    }
    
     public Vector viewFeeds (String cName){
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        Vector<Feed> fList;
        fList = new Vector<Feed>();
        
        getDbcon().connect();
        query = "SELECT id, characterName, dateUpdated, description, charInv, battleId FROM Feed WHERE characterName = ? ORDER BY id DESC";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, cName);
            rs = stmt.executeQuery();	
        
            while (rs.next())
            {	
                Feed f = new Feed();
                f.setId(rs.getInt("id"));
                f.setCharacterName(rs.getString("characterName").trim());
                f.setDateUpdated(rs.getString("dateUpdated").trim());
                f.setDescription(rs.getString("description").trim());
                f.setCharInv(rs.getString("charInv").trim());
                f.setBattleId(rs.getInt("battleId"));
                fList.add(f);
             }
            //crset.populate(rs);
            rs.close(); 					
            stmt.close();
            return fList;
 				
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
       finally {
            getDbcon().disconnect();
        }
        return null;
     }

     public Vector viewFeeds1 (String cName){
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        Vector<Feed> fList;
        //int count = 0;
            
        fList = new Vector<Feed>();
                
        getDbcon().connect();
        query = "SELECT Feed.id, Feed.characterName, Feed.dateUpdated, Feed.description, Feed.charInv, Feed.battleId FROM Feed, Comrade WHERE Comrade.characterName = ? AND Comrade.friendName = Feed.characterName ORDER BY dateUpdated DESC";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, cName);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
                Feed f = new Feed();
                f.setId(rs.getInt("id"));
                f.setCharacterName(rs.getString("characterName").trim());
                f.setDateUpdated(rs.getString("dateUpdated").trim());
                f.setDescription(rs.getString("description").trim());
                f.setCharInv(rs.getString("charInv").trim());
                f.setBattleId(rs.getInt("battleId"));
                fList.add(f);
               
             }
            //crset.populate(rs);
            rs.close(); 					
            stmt.close();
            return fList;
 				
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
       finally {
            getDbcon().disconnect();
        }
        return null;
     }
     
    public dbConnection getDbcon() {
        return dbcon;
    }

    public void setDbcon(dbConnection dbcon) {
        this.dbcon = dbcon;
    }

    public String getCharacterName() {
        return characterName;
    }

    public void setCharacterName(String characterName) {
        this.characterName = characterName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDateUpdated() {
        return dateUpdated;
    }

    public void setDateUpdated(String dateUpdated) {
        this.dateUpdated = dateUpdated;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCharInv() {
        return charInv;
    }

    public void setCharInv(String charInv) {
        this.charInv = charInv;
    }

    public int getBattleId() {
        return battleId;
    }

    public void setBattleId(int battleId) {
        this.battleId = battleId;
    }
}
